/**
 * Created by developer on 17-2-27.
 */
var $newUserForm = $(".container-users-new");
var $cover = $(".masking");
var $rightSilder = $(".aside-slider-content");
var $button = $("#users_status .switch-animate");
var $test = true;
$(function () {
    //点击新建用户
    $(".add-users-new").on("click", function () {
        modifyDisplayFlex($newUserForm);
        $newUserForm.find(".panel_header").html("<span>新建用户</span><span class='cancel glyphicon glyphicon-remove'></span>");
    });
    //关闭新建用话
    $newUserForm.on("click", ".cancel",  function () {
        modifyDisplayNone($newUserForm);
    })
    //管理范围
    $("#radio_all").bind("click", function () {
        // console.log("error 01");
        $(".add-specific-department").css("display", "none");
    });

    $("#radio_specified").bind("click", function () {
        console.log("error 02");
        $(".add-specific-department").css("display", "-webkit-flex");
    });
    $(".add-department-special").click(function () {
        $(".modal-select-users").css("display","flex");
    });
    $(".modal-select-users").find(".cancel").click(function () {
        $(".modal-select-users").css("display","none");
    });
    $(".list .list-item").on("click", function () {
        if ($rightSilder.css("right") == "-400px") {
            $rightSilder.animate({
                right:0
            })
        } else {
            $rightSilder.animate({
                right:"-400px"
            })
        }
    });
    $rightSilder.find(".cancel").click(function () {
        $rightSilder.animate({
            right:"-400px"
        });
    });

    //点击重置密码
    $(".button-change-password").click(function () {
        modifyDisplayFlex($(".modal-reset-password"));
    })

    $(".modal-reset-password").find(".cancel").click(function () {
        modifyDisplayNone($(".modal-reset-password"));
    })

    //点击删除用户

    $(".button-user-delete").click(function () {
        modifyDisplayFlex($(".container-delete-user"));
    })
    $(".container-delete-user").find(".cancel").click(function () {
        modifyDisplayNone($(".container-delete-user"));
    })

    //点击编辑
    $(".button-user-modify").on("click", function () {
        modifyDisplayFlex($newUserForm);
        $newUserForm.find(".panel_header").html("<span>修改用户</span><span class='cancel glyphicon glyphicon-remove'></span>");

    });
    //关闭新建用话
    $newUserForm.on("click",".cancel",function () {
        modifyDisplayNone($newUserForm);
    })

    //小眼睛换颜色
    $(".alter-password-visiable").bind("click", function () {
        if ($(this).css("color") == "rgb(146, 156, 168)") {
            $(this).css("color", "rgb(74, 144, 226)");
            $(this).siblings("input").attr("type", "text");
        } else {
            $(this).css("color", "#929ca8");
            $(this).siblings("input").attr("type", "password");
        }
    });

    $(".modal-large h2 button").bind("click", function () {
        var _this = this;
        console.log($(this).index());
        clickSearchBlockStatusSwitchButton(_this);
        $(".tree-users-select").css("display", "none");
        $(".users-select-position").css("display", "flex");
        $(".tree-users-select").eq($(this).index()).css("display", "flex");
        if ($(this).index() == 1) {
            $(".tree-users-select").eq($(this).index()).children()
        }
    });

    //选择成员
    $(".selected-contacts-user").click(function () {
        $(".modal-select-users").css("display","flex");
    })

    $(".jstree-node .jstree-ocl").on("click", function () {
        var $bro_ul = $(this).siblings(".jstree-children");
        modifyIconStatus($(this).parent(".jstree-node"), $bro_ul);
    });

    //状态切换
    $button.on("click", function (e){
        console.log("Point");
        if ($test) {
            $(this).animate({left: "-50px"});
            $(this).removeClass("switch-on");
            $(this).addClass("switch-off");
            $test = false;
        }else{
            $(this).animate({left: "0px"});
            $(this).removeClass("switch-off");
            $(this).addClass("switch-on");
            $test = true;
        }
        e.stopPropagation();
    });
});


//修改css样式display为flex
function modifyDisplayFlex(obj) {
    obj.css("display", "flex");
    $cover.css("display", "block");
}
//修改css样式display为none
function modifyDisplayNone(obj) {
    obj.css("display", "none");
    $cover.css("display", "none");
}

function clickSearchBlockStatusSwitchButton(val) {
    // 将所有状态按钮设为取消点击
    $(val).parent().children().removeClass("active");
    // 将当前按钮设为已点击
    $(val).addClass("active");
}

function modifyIconStatus(obj, bro) {
    if (modify(obj, bro)) {
        obj.removeClass("jstree-open");
        obj.addClass("jstree-closed");
        bro.css("display", "none");
    } else {
        obj.removeClass("jstree-closed");
        obj.addClass("jstree-open");
        bro.css("display", "block");
    }
}

function modify(obj, bro) {
    if (bro.css("display") == "none") {//判断兄弟元素是否出现，如果没出现
        return oTest = false;
    } else {
        return oTest = true;
    }
}